<script type="text/javascript">
    function addCredit() {
        document.getElementById('blockCredit').style.display = "block";
        document.getElementById('blockCreditSub').style.display = "none";
    }

    function subCredit() {
        document.getElementById('blockCreditSub').style.display = "block";
        document.getElementById('blockCredit').style.display = "none";
    }

    $( function(){
        //Increase Credit
        $("#btadd").click(function(){
            var credit = $("#credit").val();
            var userid = $("#userid").val();
            var totalcredit = $("#totalcredit").val();
            var total = parseInt(totalcredit) + parseInt(credit);
            if(credit > 0) {
                if (confirm('Bạn có muốn tăng thêm credit cho user?')) {
                    $.ajax({
                        type: "POST",
                        url: "/backend/client/ajaxaddcredit",
                        data: {credit:credit, userid:userid},
                        success: function(data){
                            if(data == 'true') {
                               $('#nameCredit').html(total);
                               $('#credit').val('');
                               $('#totalcredit').val(total);
                            }
                        }
                    });
                }
            }
            return;
        });

        //Decrease Credit
        $("#btsub").click(function(){
            var credit = $("#credit1").val();
            var userid = $("#userid").val();
            var totalcredit = $("#totalcredit").val();
            var total = parseInt(totalcredit) - parseInt(credit);
            
            if(credit > 0 && total > credit) {
                if (confirm('Bạn có muốn giảm credit của user này?')) {
                    $.ajax({
                        type: "POST",
                        url: "/backend/client/ajaxsubcredit",
                        data: {credit:credit, userid:userid},
                        success: function(data){
                            if(data == 'true') {
                               $('#nameCredit').html(total);
                               $('#credit1').val('');
                               $('#totalcredit').val(total);
                            }
                        }
                    });
                }
            }
            return;
        });

    });
</script>
<h3>Balance User</h3>
<div style="padding-left: 10px">
    <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
        <tbody>
            <tr>
                <td>
                    <form name="frmSearchUser" action="" method="post">
                        <input type="text" name="username" value="Please type username" onfocus="this.value=''">
                        <input type="submit" name="search" value="Search">
                    </form>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<BR/><BR/>
<div style="padding-left: 10px">
    <table border="0" width="100%" cellpadding="0" cellspacing="0">
        <tbody>
            <tr>
                <td>
                    <ul style="list-style: none">
                        <li><strong>UserID:</strong>&nbsp;&nbsp;&nbsp;<?=$this->arrUser['userid']?> </li>
                        <li><strong>UserName:</strong>&nbsp;&nbsp;&nbsp;<?=$this->arrUser['username']?> </li>
                        <li><strong>FullName:</strong>&nbsp;&nbsp;&nbsp;<?=$this->arrUser['fullname']?> </li>
                        <li><strong>Email:</strong>&nbsp;&nbsp;&nbsp;<?=$this->arrUser['email']?></li>
                        <li><strong>Cell Phone:</strong>&nbsp;&nbsp;&nbsp;<?=$this->arrUser['cellphone']?></li>
                        <li><strong>Total Credit:</strong>&nbsp;&nbsp;&nbsp;<span id="nameCredit"><?=($this->arrBalance['totalcredit'])?$this->arrBalance['totalcredit']:0?></span></li>
                        <li><a href="javascript:void(0);" onclick="addCredit();">Add balance to this user</a></li>
                        <li><a href="javascript:void(0);" onclick="subCredit();">Decrease balance of this user</a></li>
                        <li>
                            <div id="blockCredit" style="display: none">
                                <input type="text" name="credit" id="credit" value="" />
                                <input type="button" id="btadd" name="btadd" value="Add Credit" />
                            </div>
                            <div id="blockCreditSub" style="display: none">
                                <input type="text" name="credit1" id="credit1" value="" />
                                <input type="button" id="btsub" name="btsub" value="Decrease Credit" />
                            </div>
                        </li>                        
                    </ul>
                    
                    <input type="hidden" id="totalcredit" name="totalcredit" value="<?=($this->arrBalance['totalcredit'])?$this->arrBalance['totalcredit']:0?>" >
                    <input type="hidden" id="userid" name="userid" value="<?=$this->arrUser['userid']?>" >

                </td>
            </tr>
        </tbody>
    </table>
</div>
